// pages/cultureCoupons/useableStore/index.js
const common = require("../../../public.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dsort: 1,
        storeType: -1,
        couponId: '',
        pageNum: 0,
        categoryList: [],
        storeList: [],
        distanceList: [{
            text: '离我最近',
            value: 1
        },
        {
            text: '离我最远',
            value: 2
        }],
        refreshing: false,
        noMoreData: false,
        isLoadingBusy: false,
        type:""
    },
    onLoad(opt) {
        const that = this
        console.log(opt,"5458测试数据")
        getApp().getLocation(function (data) {
            that.setData({
                type:opt.type,
                couponId: opt.couponId,
                xpoint: data.longitude,
                ypoint: data.latitude
            }, function () {
                that.getStoreList(true)
            })
        })
        this.getStoreTypeList()
    },
    getStoreList(refresh = false) {
        if (refresh) {
            this.setData({
                refreshing: true,
                noMoreData: false
            })
        } else {
            if (this.data.noMoreData || this.data.isLoadingBusy) {
                return
            }
            this.setData({
                isLoadingBusy: true
            })
        }

        const pageNum = refresh ? 1 : this.data.pageNum + 1

        const that = this
        const tempJson = {
            couponId: that.data.couponId,
            regionId: '',
            pageNum,
            dsort: that.data.dsort,
            xpoint: that.data.xpoint,
            ypoint: that.data.ypoint,
            storeType: that.data.storeType,
        }
        common.post('/discountCoupon/showAdminCouponApplyStore', tempJson, function (res) {
            if (res.status === 200) {
                that.data.pageNum = pageNum
                if (res.data && res.data.length > 0) {
                    that.setData({
                        storeList: refresh ? res.data : [...that.data.storeList, ...res.data]
                    })
                } else {
                    that.setData({
                        noMoreData: true
                    })
                }
            } else {
                common.showRequestErrMsg(res)
            }
        }, function () {
            that.setData({
                refreshing: false,
                isLoadingBusy: false
            })
        })
    },
    distanceChange(e) {
        const value = e.detail
        this.setData({
            dsort: value
        }, _ => {
            this.getStoreList(true)
        })
    },
    categoryChange(e) {
        const value = e.detail
        this.setData({
            storeType: value
        }, _ => {
            this.getStoreList(true)
        })
    },
    getStoreTypeList() {
        const that = this
        common.post('/store/getUserFullStoreTypeList', {}, function (res) {
            if (res.status === 200) {
                let categoryList = res.data.map(function (value, index, array) {
                    return {
                        value: value.id,
                        text: value.name
                    }
                })

                categoryList.unshift({
                    value: -1,
                    text: '不限'
                })

                that.setData({
                    categoryList
                })
            } else {
                common.showRequestErrMsg(res)
            }
        })
    },
    toStore(e) {
        const item = e.currentTarget.dataset.item
        const storeTypeName = e.currentTarget.dataset.item.storeTypeName
        const id = item.id

        if (storeTypeName === '景区景点') {
            wx.navigateTo({
                url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=1',
            })
        } else if (storeTypeName === '文化馆' || storeTypeName === '美术馆' || storeTypeName === '博物馆') {
            wx.navigateTo({
                url: '/pages/ScenicArea/ScenicAreaMainPage/index?id=' + id + '&moduleType=2',
            })
        } else {
            common.gotoStore(item)
        }
    },
    callPhone(e) {
        let mobile = e.currentTarget.dataset.mobile
        wx.makePhoneCall({
            phoneNumber: mobile,
        })
    },
    onReachBottom() {
      
        this.getStoreList(false)
    }
})